<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" cancelButton="none" :radius="100"></uni-search-bar>
    </view>
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" v-for="(item,i) in  searchResults" :key="i" @click="handleDetail(item)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history_box" v-else>
      <!-- 标题 -->
      <view class="history_title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
      </view>
      <!-- 标题 -->
      <!-- 列表 -->
      <view class="history_list">
        <uni-tag :text="item" v-for="(item,i) in histories" :key="i" class="history_list_item" @click="gotoGoodList(item)"></uni-tag>
      </view>
      <!-- 列表 -->
    </view>
    <!-- 搜索历史 -->
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:0,
        kw:[],
        searchResults:[],
        historyData:[]
      };
    },
    // 页面持续化存储
    onLoad() {
      this.historyData = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    methods:{
      input(e){
        clearTimeout(this.timer)
        this.timer =  setTimeout(()=>{
          // console.log(e.value)
          this.kw = e.value
          this.getSearchResults()
        },500)
      },
      async getSearchResults(){
        if(this.kw.length == 0){
          this.searchResults = []
          return
        }
        const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
        if(res.meta.status !==200) uni.$showMsg()
        this.searchResults = res.message
        this.saveHistoryData()
      },
      handleDetail(item){
        // console.log(item)
        uni.navigateTo({
          url:'/subpkg/good_detail/good_detail?cid='+item.goods_id
        })
      },
      saveHistoryData(){
        // this.historyData.push(this.kw)
        const set = new Set(this.historyData)
        set.delete(this.kw)
        set.add(this.kw)
        this.historyData = Array.from(set)
        // console.log(set)
        // 持续化存储到本地
        uni.setStorageSync('kw',JSON.stringify(this.historyData))
      },
      cleanHistory(){
        this.historyData = []
        uni.setStorageSync('kw',[])
      },
      gotoGoodList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+item
        })
      }
    },
    computed:{
      histories(){
        return [...this.historyData].reverse()
      }
    }
  }
</script>

<style lang="scss">
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-item{
    display: flex;
    justify-content: space-between;
    // 垂直居中
    align-items: center;
    // text-align: center;
    // vertical-align: middle;
    font-size: 12px;
    padding: 12px 0;
    border-bottom: 1px solid #ccc;
    .goods-name{
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
  .history_box{
    
    .history_title{
      display: flex;
      justify-content: space-between;
      padding: 10px 10px;
      border-bottom: 1px solid #ccc;
    }
    .history_list{
      display: flex;
      flex-wrap: wrap;
      .history_list_item{
        margin: 3px 5px;
      }
    }
  }
</style>
